﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;
using WMSABL;
using WMSABL.SY;
using WMSAML.SY;
using WMSABL.Warehouse;

namespace ElectronicWMSA
{
    public partial class PickNO_MapFrm : Form
    {
        private string strWhere = "";
        private int PD_LayoutNID = 0;
        private string PD_LayoutField = "";
        private string WarehouseCode = "";
        private string Area = "";
        private string Postion = "";
        private int SelectRowindex = -1;
        DataSet ds = new DataSet();
        PD_LayoutUpdateBLL bll = new PD_LayoutUpdateBLL();
        PublicDataSet bll_PublicDataSet = new PublicDataSet();
        PublicDataSet_SAP bll_PublicDataSet_SAP = new PublicDataSet_SAP();
        public PickNO_MapFrm()
        {
            InitializeComponent();
            this.dataGridView1.AllowUserToResizeColumns = false;
            this.dataGridView1.AllowUserToResizeRows = false;
        }
        #region
        private void Warehouse_PickNOFrm_Load(object sender, EventArgs e)
        {
            this.strWhere = "";
            this.pageControl1.PageCurrent = 1;
            this.pageControl1.PageSize = 500;
            this.pageControl1.Bind();
            this.WarehouseCode = "【仓库:" + EWMSAClass.WMS_Number + "】";
            this.label1.Text = WarehouseCode;
            this.txtProductName.Focus();
        }
        private int DBind()
        {
            ElectronicWMSA.Comm.Forms.MsControls.PageData pageData = new ElectronicWMSA.Comm.Forms.MsControls.PageData();
            pageData.TableName = "PD_Layout";
            pageData.PrimaryKey = "NID";
            pageData.OrderStr = "NID";
            pageData.PageIndex = this.pageControl1.PageCurrent;
            pageData.PageSize = this.pageControl1.PageSize;
            pageData.QueryCondition = strWhere;
            pageData.QueryFieldName = "*";

            this.pageControl1.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            this.pageControl1.bindingNavigator.BindingSource = pageControl1.bindingSource;
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = this.pageControl1.bindingSource;
            SystemVariable.DataGridViewStyle(dataGridView1);
            return pageData.TotalCount;
        }
        private int pageControlU_EventPaging(ElectronicWMSA.Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBind();
        }
        private void dataGridViewUserList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            SelectRowindex = -1;
            if (e.RowIndex > -1 && e.ColumnIndex > 1)
            {
                SelectRowindex = e.RowIndex;
                PD_LayoutNID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["NID"].Value);
                PD_LayoutField = dataGridView1.Columns[e.ColumnIndex].HeaderText.ToString();
                if (dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[e.ColumnIndex].Value != null)
                {
                    Area = dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[e.ColumnIndex].Value.ToString();
                    Postion = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                }
            }
        }
        #region
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex != -1)
            {
                if (Postion != "" && Postion != "===========")
                {
                    this.dataGridView1.Rows[SelectRowindex].DefaultCellStyle.BackColor = System.Drawing.Color.Gainsboro;
                    ds = new DataSet();
                    ds = bll_PublicDataSet.GetDataSet("select  a.PositionNO,a.ProductNO,a.Quantity,a.Unit from PD_Stock as a left join PD_PostionToLayout as b  on b.PostionNO=a.PositionNO where  b.Layout_NID='" + PD_LayoutNID + "' and b.Layout_Field='" + PD_LayoutField + "' ");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        ElectronicWMSA.Warehouse.Postion_LayoutDataFrm frm = new ElectronicWMSA.Warehouse.Postion_LayoutDataFrm(ds);
                        frm.ShowDialog();
                        this.dataGridView1.ClearSelection();
                    }
                    else
                    {
                        MessageClass.warning("此仓位没有信息！");
                    }
                }
            }
        }
        #endregion

        #endregion

        #region  查询定位
        PD_PickingListBLL bll_PD_PickingList = new PD_PickingListBLL();
        private bool IsLocationPostiont = false;
        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.pageControl1.Bind();
            this.labelGO.BackColor = System.Drawing.Color.Transparent;
            this.LocationPostiont();
        }
        private void LocationPostiont()
        {
            ds = new DataSet();
            if (txtProductName.Text.Trim() == "")
            {
                MessageClass.warning("拣货单号不能为空！");
                this.pageControl1.Bind();
            }
            else if (this.txtProductName.Text.Trim() != "")
            {
                ds = new DataSet();
                ds = bll_PublicDataSet.GetDataSet("SELECT COUNT(NID) FROM dbo.PD_PickingList WHERE PickNO='" + txtProductName.Text.Trim() + "' ");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ds = new DataSet();
                    ds = bll_PublicDataSet.GetDataSet("SELECT A.Layout_NID,A.Layout_Field from PD_PostionToLayout AS A LEFT JOIN PD_Stock AS B ON A.PositionNO=B.PositionNO LEFT JOIN PD_PickingList AS C ON c.ProductNo=b.ProductNo WHERE C.PickNO='" + txtProductName.Text.Trim() + "' ");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            for (int j = 0; j < dataGridView1.RowCount; j++)
                            {
                                if (dataGridView1.Rows[j].Cells["NID"].Value.ToString() == ds.Tables[0].Rows[i]["Layout_NID"].ToString())
                                {
                                    for (int y = 0; y < dataGridView1.ColumnCount; y++)
                                    {
                                        if (dataGridView1.Columns[y].HeaderText == ds.Tables[0].Rows[i]["Layout_Field"].ToString())
                                        {
                                            this.dataGridView1.Rows[j].Cells[y].Style.BackColor = System.Drawing.Color.Cyan;
                                            break;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        DBindCombox();
                        IsLocationPostiont = true;
                    }
                    else
                    {
                        MessageClass.warning("未能检测到库存信息，请核对是否分配库存！");
                        this.pageControl1.Bind();
                    }
                }
                else
                {
                    MessageClass.warning("未能检测到拣货单信息，请进行》拣货单核查！");
                    this.pageControl1.Bind();
                }
            }
        }
        #region
        public void DBindCombox()
        {
            DataSet ds = new DataSet();
            ds = bll_PublicDataSet.GetDataSet(" SELECT userName FROM dbo.PD_PickingList WHERE PickNO='170214004' GROUP BY userName ");
            this.comBoxName.DataSource = ds.Tables[0];
            this.comBoxName.ValueMember = "NID";
            this.comBoxName.DisplayMember = "userName";
        }
        #endregion

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtProductName.Text = "";
            this.IsLocationPostiont = false;
            this.labelGO.BackColor = System.Drawing.Color.Transparent;
            this.pageControl1.Bind();
        }
        #endregion

        #region 取货线路显示
        private void btnLine_Click(object sender, EventArgs e)
        {
            if (IsLocationPostiont)
                this.DrawLines();
            else
                MessageClass.warning("请先对拣货单定位，再查询线路图！");
        }
        List<Point> Posiont_listPoint = new List<Point>();
        List<Point> list2 = new List<Point>();
        List<Point> lists = new List<Point>();
        private void DrawLines()
        {
            this.Posiont_listPoint.Clear();
            ds = new DataSet();
            ds = bll_PublicDataSet.GetDataSet("select distinct PointTop from PD_PostionToLayout as A left join PD_Stock as B on A.PositionNO=B.PositionNO left join PD_PickingList as C on c.ProductNo=b.ProductNo where C.PickNO='" + txtProductName.Text.Trim() + "' ");
            string[] valuesstr = null;
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    valuesstr = ds.Tables[0].Rows[i]["PointTop"].ToString().Split(',');
                    Posiont_listPoint.Add(new Point(Convert.ToInt32(valuesstr[0]), Convert.ToInt32(Convert.ToInt32(valuesstr[1]))));
                }
                DrawLinesMap(Posiont_listPoint);
            }
        }
        private void DrawLinesMap(List<Point> listarray)
        {
            Point px = new Point(0, 0);
            px = labelGO.PointToScreen(px);
            px = new Point(px.X + labelGO.Width / 2, px.Y + labelGO.Height / 2);
            var lst_Permutation = PermutationAndCombination<Point>.GetPermutation(listarray.ToArray(), listarray.Count);
            lst_Permutation.ToArray();
            double leg1 = 0;
            double leg2 = 0;
            int iii = 0;
            for (int i = 0; i < lst_Permutation.Count; i++)
            {
                var ss = lst_Permutation[i];
                list2.Clear();
                list2.Add(px);
                for (int j = 0; j < ss.Length; j++)
                {
                    list2.Add(ss[j]);
                }
                leg1 = GetLeg(list2);
                if (leg2 == 0)
                {
                    leg2 = leg1;
                    lists = new List<Point>();
                    for (int k = 0; k < list2.Count; k++)
                    {
                        lists.Add(list2[k]);
                    }
                }
                else if (leg1 < leg2)
                {
                    leg2 = leg1;
                    lists = new List<Point>();
                    for (int k = 0; k < list2.Count; k++)
                    {
                        lists.Add(list2[k]);
                    }
                }
                else if (leg1 == leg2)
                {
                    iii++;
                }
            }
            Pen blackPen = new Pen(Color.Red, 3);
            this.dataGridView1.CreateGraphics().DrawLines(blackPen, lists.ToArray());
            this.labelGO.BackColor = System.Drawing.Color.Red;
        }
        private double GetLeg(List<Point> listP1)
        {
            double result = 0;
            if (listP1.Count > 0)
            {
                for (int i = 0; i < listP1.Count; i++)
                {
                    if (i + 1 < listP1.Count)
                    {
                        result += GetValue(listP1[i], listP1[i + 1]);
                    }
                }
            }
            return Math.Round(result, 2);
        }
        private double GetValue(Point p1, Point p2)
        {
            return Math.Round(Math.Sqrt(Math.Abs(p1.X - p2.X) * Math.Abs(p1.X - p2.X) + Math.Abs(p1.Y - p2.Y) * Math.Abs(p1.Y - p2.Y)), 2);
        }
        private void BtnClearLine_Click(object sender, EventArgs e)
        {
            this.labelGO.BackColor = System.Drawing.Color.Transparent;
            this.dataGridView1.Invalidate();
        }
        #endregion

        #region SAP 拣货单号信息
        private void btnPickingInfo_Click(object sender, EventArgs e)
        {
            if (txtProductName.Text.Trim() == "")
            {
                MessageClass.warning("拣货单号不能为空！");
            }
            else if (txtProductName.Text.Trim() != "")
            {
                ds = new DataSet();
                ds = bll_PublicDataSet.GetDataSet("SELECT PickNO,Line,ProductNO,NeedQty FROM PD_PickingList WHERE PickNO='" + txtProductName.Text.Trim() + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ElectronicWMSA.Warehouse.Picking_ListToNumberInfoFrm frm = new ElectronicWMSA.Warehouse.Picking_ListToNumberInfoFrm(ds);
                    frm.ShowDialog();
                    this.dataGridView1.ClearSelection();
                }
                else
                {
                    MessageClass.warning("未能检测到拣货单信息，请进行》拣货单核查！");
                }
            }
        }
        #endregion

        #region
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
